<template>
  <view class="login">
    <input v-model="username" placeholder="用户名" />
    <input v-model="password" type="password" placeholder="密码" />
    <button @click="login">登录</button>
  </view>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: ''
    };
  },
  methods: {
    async login() {
      try {
        const res = await this.$api.post('/auth/login', {
          username: this.username,
          password: this.password
        });
        if (res.success) {
          uni.setStorageSync('user_token', res.data.token);
          uni.showToast({
            title: '登录成功',
            icon: 'success'
          });
          uni.switchTab({
            url: '/pages/my/my'
          });
        } else {
          uni.showToast({
            title: res.message || '登录失败',
            icon: 'none'
          });
        }
      } catch (err) {
        console.error(err);
        uni.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    }
  }
};
</script>